﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace RaisingForce.Raise.Presentation.Controls
{
    public class ModalDialogBase :
        UserControl
    {
        #region Constructors

        public ModalDialogBase()
            : base()
        { }

        #endregion

        #region Private Declarations

        private bool parentWasEnabled = true;

        private bool manualDialogInvisibility = false;

        private void checkOverlayOnProperty()
        {
            if (this.OverlayOn == null)
            {
                throw new InvalidOperationException(
                    "Required properties are not bound to the model.");
            }
        }

        private void show()
        {
            // Force recalculate binding since Show can be called before binding are calculated            
            BindingExpression expressionOverlayParent = this.GetBindingExpression(OverlayOnProperty);
            if (expressionOverlayParent != null)
            {
                expressionOverlayParent.UpdateTarget();
            }
            this.checkOverlayOnProperty();
            this.parentWasEnabled = this.OverlayOn.IsEnabled;
            this.OverlayOn.IsEnabled = false;
            this.Visibility = Visibility.Visible;
        }

        private void hide()
        {
            this.checkOverlayOnProperty();
            if (!this.manualDialogInvisibility)
            {
                this.Visibility = Visibility.Hidden;
            }
            this.OverlayOn.IsEnabled = this.parentWasEnabled;
        }

        #endregion

        #region Protected Declarations

        protected void InitializeDialog(
            bool manualDialogInvisibility = false)
        {
            this.manualDialogInvisibility = manualDialogInvisibility;
            this.Visibility = Visibility.Hidden;
        }

        #endregion

        #region IsShown Dependency Property

        public bool IsShown
        {
            get { return (bool)GetValue(IsShownProperty); }
            set { SetValue(IsShownProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsShown.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsShownProperty =
            DependencyProperty.Register(
                "IsShown",
                typeof(bool),
                typeof(ModalDialogBase),
                new UIPropertyMetadata(false, isShownChangedCallback));

        private static void isShownChangedCallback(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            ModalDialogBase dialog = (ModalDialogBase)d;
            if ((bool)e.NewValue == true)
            {
                dialog.show();
            }
            else
            {
                dialog.hide();
            }
        }

        #endregion

        #region OverlayOn Dependency Property

        public UIElement OverlayOn
        {
            get { return (UIElement)GetValue(OverlayOnProperty); }
            set { SetValue(OverlayOnProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Parent.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OverlayOnProperty =
            DependencyProperty.Register(
                "OverlayOn",
                typeof(UIElement),
                typeof(ModalDialogBase),
                new UIPropertyMetadata(null));

        #endregion
    }
}
